package cn.dnui_zll731.dnuifood_731zlll.controller;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import cn.dnui_zll731.dnuifood_731zlll.Adapter.FoodInfoAdapter;
import cn.dnui_zll731.dnuifood_731zlll.bean.Result;
import cn.dnui_zll731.dnuifood_731zlll.common.Constants;
import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.CommentsBean;
import cn.dnui_zll731.dnuifood_731zlll.bean.FoodInfoBean;
import cn.dnui_zll731.dnuifood_731zlll.bean.isCollected;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.FoodModel;
import cn.dnui_zll731.dnuifood_731zlll.model.UserModel;
public class FoodInfoActivity extends BaseActivity implements RetrofitListener {
    private RecyclerView recyclerView;
    private ImageButton fanhui,shoucang;
    private Button goumai,contact;
    private ImageView pic;
    private TextView food_name,price,intro,shopname;

    private Intent intent;
    private String food_id;
    private FoodModel foodModel;
    private FoodInfoAdapter foodInfoAdapter;
    private String result,phonenum;
    private FoodInfoBean foodInfoBean;

    @Override
    protected int initlayout() {

        return R.layout.activity_foodinfo;
    }
    @Override
    void initViews() {

        recyclerView = findViewById(R.id.info_rv);
        fanhui =  findViewById(R.id.info_fanhui);
        shoucang= findViewById(R.id.info_sc);
        goumai =  findViewById(R.id.info_goumai);

        pic = findViewById(R.id.info_pic);
        contact=findViewById(R.id.contact);
        shopname = findViewById(R.id.info_shopname);
        food_name = findViewById(R.id.info_food_name);
        price = findViewById(R.id.info_food_price);
        intro = findViewById(R.id.info_intro1);

    }

    @Override
    void initEvents() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(
                this,
                LinearLayoutManager.VERTICAL,
                false
        );//布局效果---垂直布局
        recyclerView.setLayoutManager(linearLayoutManager);//设置效果
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画效果


        //点击事件--返回
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //点击事件--收藏
        shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });
        //点击事件--购买
        goumai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开购买界面
                Intent intent1 = new Intent(FoodInfoActivity.this,PurchaseActivity.class);
                intent1.putExtra("shop_id",foodInfoBean.getShop_id());
                intent1.putExtra("food_id",foodInfoBean.getFood_id());
                intent1.putExtra("food_name",foodInfoBean.getFoodname());
                intent1.putExtra("price",foodInfoBean.getPrice());
                intent1.putExtra("pic",foodInfoBean.getPic());
                startActivity(intent1);
            }
        });

        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开拨号界面
                Uri uri = Uri.parse("tel:"+phonenum);
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                if (ActivityCompat.checkSelfPermission(FoodInfoActivity.this,Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED){
                    startActivity(intent);
                    return;
                }
            }
        });
    }

    private void collect() {
        //调user的收藏接口   收藏菜品
        UserModel userModel = new UserModel();
        userModel.userCollectFood(getUser_id(),food_id,this);
    }

    @Override
    void initData() {
        //获取上层传来的商家电话
        intent = getIntent();
        phonenum=intent.getStringExtra("phonenum");
        food_id = intent.getStringExtra("food_id");
        shopname.setText(intent.getStringExtra("shop_name"));
        //获取用户id以及填充菜谱信息
        showFoodInfo();
        //获取菜谱评价信息
        showComments();
        //获取菜谱收藏状态
        iscollected();

    }

    private void iscollected() {   //--------------获取菜谱收藏状态
        foodModel = new FoodModel();
        foodModel.isCollected(getUser_id(),food_id,"1",this);
    }

    private void showComments() { //--------------------获取菜谱评价信息
        foodModel = new FoodModel();
        foodModel.getAllCommentsByFood(food_id,this);
    }

    private void showFoodInfo() {    //------------获取用户id以及填充菜谱信息
        foodModel = new FoodModel();
        foodModel.getFoodById(food_id,this);
    }



    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case Constants.GETFOODBYID:
                foodInfoBean = (FoodInfoBean)object;
                food_name.setText(foodInfoBean.getFoodname());
                price.setText("￥"+foodInfoBean.getPrice()+"元");
                intro.setText("菜品简介："+foodInfoBean.getIntro());
                String str_pic = foodInfoBean.getPic();
                if (!str_pic.equals("")) {
                    Picasso.with(this).load(Constants.BASE_URL + str_pic).into(pic);
                }
                break;
            case Constants.GETALLCOMMENTSBYFOOD://获取评论信息
                foodInfoAdapter = new FoodInfoAdapter(this,(List<CommentsBean>)object,R.layout.info_item);
                recyclerView.setAdapter(foodInfoAdapter);
                break;
            case Constants.ISCOLLECTED://判断收藏信息
                result = ((isCollected)object).getCollected();
                if (result.equals("0")) {
                    shoucang.setImageResource(android.R.drawable.btn_star_big_off);
                }else {
                    shoucang.setImageResource(android.R.drawable.btn_star_big_on);
                }
                break;
            case Constants.USERCOLLECTFOOD:
                if (((Result)object).getSuccess().equals("1")){
                    if (result.equals("0")){
                        showToast("收藏成功");
                        shoucang.setImageResource(android.R.drawable.btn_star_big_on);
                        result="1";
                    }else {
                        showToast("取消收藏成功");
                        shoucang.setImageResource(android.R.drawable.btn_star_big_off);
                        result = "0";
                    }
                }else {
                    showToast("操作失败");
                }
                break;
        }
    }

    @Override
    public void onFalure() {
        showToast("网络错误");
    }
}